package com.atguigu.srb.jwt;

import io.jsonwebtoken.*;
import org.junit.Test;

import java.util.Timer;

/**
 * @program: srb
 * @author: 龙龙
 * @create: 2022-02-11 16:05
 * @description:
 **/
public class test01 {
    String serviceKey = "slx66+ASDDDDDDDDDDDWADASDASDWSEFER6%&^*^&*$%^#$";

    @Test
    public void c() {
        //加入客户端盐
        String ip = "127.0.0.4";
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuaWNrbmFtZSI6IuWbvei2s-a1t-WPgiIsImF2YXRhciI6IjEuanBnIn0.iqMoVzWE_Bf4jFW36AulNODhqcNiyFy_ibR2GcW7z_k";

//        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(MD5.encrypt(serviceKey+ip)).parseClaimsJws(token);
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(serviceKey+ip).parseClaimsJws(token);
        System.out.println(claimsJws);

    }


    @Test
    public void b() {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuaWNrbmFtZSI6IuWbvei2s-a1t-WPgiIsImF2YXRhciI6IjEuanBnIn0.Buj1-j4HerV6_KmucMj5RmXJS0gQhXD1wZHlDy0Ntfs";

        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(serviceKey).parseClaimsJws(token);
        System.out.println(claimsJws);

    }


    @Test
    public void a() {
        //用户
        User user = new User();
        user.setUsername("国足海参");
        user.setAvatar("1.jpg");

        JwtBuilder builder = Jwts.builder();
        //头信息
        builder.setHeaderParam("typ", "JWT");
        builder.setHeaderParam("alg", "HS256");

        //核载信息
        builder.claim("nickname", user.getUsername());
        builder.claim("avatar", user.getAvatar());

        //签名信息
        String ip = "127.0.0.1";
//        builder.signWith(SignatureAlgorithm.HS256, MD5.encrypt(serviceKey+ip));
        builder.signWith(SignatureAlgorithm.HS256, serviceKey+ip);

        //生成token
        String token = builder.compact();
        System.out.println(token);


    }
}
